package com.mobi.cacti.ns.view;

import java.util.ArrayList;

import org.holoeverywhere.LayoutInflater;
import org.holoeverywhere.app.Fragment;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.ProgressBar;

import com.mobi.cacti.R;
import com.mobi.cacti.ns.model.ListItem;
import com.mobi.cacti.ns.view.adapter.ItemListAdapter;
import com.mobi.cacti.ns.view.tab.BaseContainerFragment;
import com.mobi.cacti.rabbit.controllers.NewsAndPromotionListActivityViewController;
import com.mobi.cacti.rabbit.controllers.NewsAndPromotionListActivityViewControllerCallback;
import com.mobi.cacti.rabbit.response.ReqRssItem;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class NewsPromotionsListActivity extends Fragment implements NewsAndPromotionListActivityViewControllerCallback{
	
	private NewsAndPromotionListActivityViewController newsAndPromotionListActivityViewController;
	
	private ArrayList<ReqRssItem> rssItems;
	private ArrayList<ListItem> items;
	
	private ItemListAdapter adapter;
	
	private ProgressBar progressBar;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		newsAndPromotionListActivityViewController = NewsAndPromotionListActivityViewController.getInstance();
		newsAndPromotionListActivityViewController.delegate = this;
		
		items = new ArrayList<ListItem>();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Bundle bundle = this.getArguments();
		String rssType = bundle.getString("rss_type");
		
		if (rssType.equals("news")) {
			newsAndPromotionListActivityViewController.loadNews(this.getActivity());
		} else if (rssType.equals("promotions")) {
			newsAndPromotionListActivityViewController.loadPromotion(this.getActivity());
		}
		
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.newspromotions_layout, null);
		
		progressBar = (ProgressBar) v.findViewById(R.id.progressbar);
		
		adapter = new ItemListAdapter(getSupportActivity(), R.layout.rowitem_layout, items);
		ListView listView = (ListView) v.findViewById(R.id.newspromotionlist);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				
				ReqRssItem rssItem = rssItems.get(position);
				
				Bundle bundle = new Bundle();
				bundle.putString("link", rssItem.link);
				
				NewsPromotionsDetailActivity newsPromotionsDetailActivity = new NewsPromotionsDetailActivity();
				newsPromotionsDetailActivity.setArguments(bundle);
				
				((BaseContainerFragment)getParentFragment()).replaceFragment(newsPromotionsDetailActivity, true);
			}
		});
		
		return v;
	}

	@Override
	public void newsAndPromotionListActivityControllerFinishLoadNews(
			ArrayList<ReqRssItem> rssItems) {
		this.rssItems = rssItems;
		reloadListView();
	}

	@Override
	public void newsAndPromotionListActivityControllerFinishLoadPromotion(
			ArrayList<ReqRssItem> rssItems) {
		this.rssItems = rssItems;
		reloadListView();
	}
	
	private void reloadListView() {
		items.clear();
		
		for(ReqRssItem rssItem : rssItems) {
			ListItem item = new ListItem();
			item.label = rssItem.title;
			item.ticketCountLabel = null;
			item.isShowRightArrow = false;
			items.add(item);
		}
		
		adapter.notifyDataSetChanged();
		
		progressBar.setVisibility(View.GONE);
	}

}
